{% extends 'todo/base.html' %}

{% block title %}待办事项列表 - 考研学习计划{% endblock %}

{% block content %}
<div class="container">
    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card stat-total">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">全部任务</h6>
                            <h2 class="mb-0">{{ total_count }}</h2>
                        </div>
                        <i class="bi bi-list-ul stat-icon"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card stat-active">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">进行中</h6>
                            <h2 class="mb-0">{{ active_count }}</h2>
                        </div>
                        <i class="bi bi-hourglass-split stat-icon"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card stat-completed">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">已完成</h6>
                            <h2 class="mb-0">{{ completed_count }}</h2>
                        </div>
                        <i class="bi bi-check-circle stat-icon"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card stat-overdue">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">已逾期</h6>
                            <h2 class="mb-0">{{ overdue_count }}</h2>
                        </div>
                        <i class="bi bi-exclamation-triangle stat-icon"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- 侧边栏 -->
        <div class="col-md-3 mb-4">
            <div class="card shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="mb-0"><i class="bi bi-filter"></i> 筛选</h5>
                </div>
                <div class="card-body p-0">
                    <!-- 状态筛选 -->
                    <div class="list-group list-group-flush">
                        <a href="?filter=all" class="list-group-item list-group-item-action {% if filter_type == 'all' %}active{% endif %}">
                            <i class="bi bi-list-ul"></i> 全部任务
                        </a>
                        <a href="?filter=active" class="list-group-item list-group-item-action {% if filter_type == 'active' %}active{% endif %}">
                            <i class="bi bi-hourglass-split"></i> 进行中
                        </a>
                        <a href="?filter=completed" class="list-group-item list-group-item-action {% if filter_type == 'completed' %}active{% endif %}">
                            <i class="bi bi-check-circle"></i> 已完成
                        </a>
                        <a href="?filter=overdue" class="list-group-item list-group-item-action {% if filter_type == 'overdue' %}active{% endif %}">
                            <i class="bi bi-exclamation-triangle"></i> 已逾期
                        </a>
                    </div>
                    
                    <!-- 科目筛选 -->
                    <div class="p-3 border-top">
                        <h6 class="mb-2"><i class="bi bi-folder"></i> 科目分类</h6>
                        <div class="list-group list-group-flush">
                            <a href="?" class="list-group-item list-group-item-action py-2 {% if not selected_category %}active{% endif %}">
                                全部科目
                            </a>
                            {% for category in categories %}
                            <a href="?category={{ category.id }}" class="list-group-item list-group-item-action py-2 d-flex justify-content-between align-items-center {% if selected_category == category.id|stringformat:'s' %}active{% endif %}">
                                <span>
                                    <span class="badge" style="background-color: {{ category.color }};">&nbsp;</span>
                                    {{ category.name }}
                                </span>
                                <span class="badge bg-secondary">{{ category.todo_count }}</span>
                            </a>
                            {% endfor %}
                        </div>
                    </div>
                    
                    <!-- 优先级筛选 -->
                    <div class="p-3 border-top">
                        <h6 class="mb-2"><i class="bi bi-flag"></i> 优先级</h6>
                        <div class="list-group list-group-flush">
                            <a href="?" class="list-group-item list-group-item-action py-2 {% if not selected_priority %}active{% endif %}">
                                全部优先级
                            </a>
                            {% for priority_code, priority_name in priority_choices %}
                            <a href="?priority={{ priority_code }}" class="list-group-item list-group-item-action py-2 {% if selected_priority == priority_code %}active{% endif %}">
                                {{ priority_name }}
                            </a>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 主内容区 -->
        <div class="col-md-9">
            <!-- 搜索和操作栏 -->
            <div class="card shadow-sm mb-3">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-md-8">
                            <form method="get" class="d-flex">
                                <input type="search" name="search" class="form-control me-2" 
                                       placeholder="搜索任务标题或描述..." 
                                       value="{{ search_query|default:'' }}">
                                <button type="submit" class="btn btn-primary">
                                    <i class="bi bi-search"></i> 搜索
                                </button>
                            </form>
                        </div>
                        <div class="col-md-4 text-end mt-2 mt-md-0">
                            <a href="{% url 'todo:create' %}" class="btn btn-success">
                                <i class="bi bi-plus-circle"></i> 新建任务
                            </a>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 待办事项列表 -->
            {% if todos %}
                {% for todo in todos %}
                <div class="card todo-card shadow-sm mb-3 {% if todo.is_completed %}completed{% endif %} {% if todo.is_overdue %}overdue{% endif %}">
                    <div class="card-body">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <div class="d-flex align-items-start">
                                    <!-- 完成状态复选框 -->
                                    <form method="post" action="{% url 'todo:toggle' todo.id %}" class="me-3">
                                        {% csrf_token %}
                                        <input type="checkbox" class="form-check-input mt-1" 
                                               {% if todo.is_completed %}checked{% endif %}
                                               onchange="this.form.submit()">
                                    </form>
                                    
                                    <div class="flex-grow-1">
                                        <h5 class="mb-1 {% if todo.is_completed %}text-decoration-line-through text-muted{% endif %}">
                                            {{ todo.title }}
                                        </h5>
                                        {% if todo.description %}
                                        <p class="text-muted mb-2 small">{{ todo.description|truncatewords:20 }}</p>
                                        {% endif %}
                                        
                                        <div class="todo-meta">
                                            <!-- 分类标签 -->
                                            {% if todo.category %}
                                            <span class="badge me-2" style="background-color: {{ todo.category.color }};">
                                                <i class="bi bi-folder"></i> {{ todo.category.name }}
                                            </span>
                                            {% endif %}
                                            
                                            <!-- 优先级 -->
                                            <span class="badge priority-{{ todo.priority }} me-2">
                                                <i class="bi bi-flag-fill"></i> {{ todo.priority_display }}
                                            </span>
                                            
                                            <!-- 截止日期 -->
                                            {% if todo.due_date %}
                                            <span class="badge {% if todo.is_overdue %}bg-danger{% else %}bg-info{% endif %} me-2">
                                                <i class="bi bi-calendar"></i> {{ todo.due_date|date:"Y-m-d H:i" }}
                                            </span>
                                            {% endif %}
                                            
                                            <!-- 创建时间 -->
                                            <span class="text-muted small">
                                                <i class="bi bi-clock"></i> {{ todo.created_at|date:"Y-m-d" }}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-md-4 text-end mt-2 mt-md-0">
                                <div class="btn-group" role="group">
                                    <a href="{% url 'todo:edit' todo.id %}" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-pencil"></i> 编辑
                                    </a>
                                    <button type="button" class="btn btn-sm btn-outline-danger" 
                                            data-bs-toggle="modal" 
                                            data-bs-target="#deleteModal{{ todo.id }}">
                                        <i class="bi bi-trash"></i> 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 删除确认模态框 -->
                <div class="modal fade" id="deleteModal{{ todo.id }}" tabindex="-1">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">确认删除</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                            </div>
                            <div class="modal-body">
                                确定要删除任务 "{{ todo.title }}" 吗？
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                <form method="post" action="{% url 'todo:delete' todo.id %}" class="d-inline">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-danger">确认删除</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            {% else %}
                <div class="card shadow-sm">
                    <div class="card-body text-center py-5">
                        <i class="bi bi-inbox text-muted" style="font-size: 4rem;"></i>
                        <h4 class="mt-3 text-muted">暂无待办事项</h4>
                        <p class="text-muted">开始创建你的第一个学习任务吧！</p>
                        <a href="{% url 'todo:create' %}" class="btn btn-primary">
                            <i class="bi bi-plus-circle"></i> 创建任务
                        </a>
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

